package com.wuyou.mybatishandlerdemo.component;

import com.alibaba.druid.support.json.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.UnsupportedEncodingException;

/**
 * EmailComponent
 *
 * @Date 2025-09-26 10:44
 * @Author 无忧
 */
@Component
@Slf4j
public class EmailComponent {
    @Autowired
    private JavaMailSender mailSender;

//    @Autowired
//    private TemplateEngine templateEngine; // 可选：用于HTML模板

    @Value("${spring.mail.username}")
    private String fromEmail; // 自动注入 yh.zhang@imelter.com

    /**
     * 发送简单文本邮件
     */
    public void sendSimpleEmail(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail); // 发件人：yh.zhang@imelter.com
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);
        System.out.println("简单邮件发送成功到: " + to);
    }

    /**
     * 发送HTML格式邮件
     */
    public void sendHtmlEmail(String to, String subject, String htmlContent) throws MessagingException, UnsupportedEncodingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

        helper.setFrom(fromEmail, "张宇航"); // 设置发件人名称
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(htmlContent, true); // true表示HTML内容

        mailSender.send(message);
        System.out.println("HTML邮件发送成功到: " + to);
    }

    /**
     * 发送带附件的邮件
     */
    public void sendEmailWithAttachment(String to, String subject, String text,
                                        String attachmentPath) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

        helper.setFrom(fromEmail);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text);

        // 添加附件
        FileSystemResource file = new FileSystemResource(new File(attachmentPath));
        helper.addAttachment(file.getFilename(), file);

        mailSender.send(message);
        System.out.println("带附件邮件发送成功到: " + to);
    }

    /**
     * 使用Thymeleaf模板发送邮件
     */
//    public void sendTemplateEmail(String to, String subject, String templateName,
//                                  Map<String, Object> variables) throws MessagingException {
//        MimeMessage message = mailSender.createMimeMessage();
//        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
//
//        // 渲染模板
//        Context context = new Context();
//        context.setVariables(variables);
//        String htmlContent = templateEngine.process(templateName, context);
//
//        helper.setFrom(fromEmail, "IMelter系统");
//        helper.setTo(to);
//        helper.setSubject(subject);
//        helper.setText(htmlContent, true);
//
//        mailSender.send(message);
//        System.out.println("模板邮件发送成功到: " + to);
//    }

    /**
     * 批量发送邮件
     */
    public void sendBatchEmails(String[] toEmails, String subject, String text) {
        for (String to : toEmails) {
            sendSimpleEmail(to, subject, text);
        }
        System.out.println("批量邮件发送完成，共发送: " + toEmails.length + " 封");
    }
}
